package com.xiaomi;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.User;
import com.qf.mapper.UserMapper;
import com.xiaomi.entity.TbUser;
import com.xiaomi.entity.TbUserExample;
import com.xiaomi.mapper.TbUserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author zjw
 * @description
 * @date 2021/9/27$ 11:38$
 */
public class Demo8Test {

    @Test
    public void mybatisGeneratorSelect() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = factory.openSession();

        // 基于SqlSession获取指定Mapper的代理对象
        TbUserMapper mapper = sqlSession.getMapper(TbUserMapper.class);

        TbUserExample tbUserExample = new TbUserExample();
        TbUserExample.Criteria criteria = tbUserExample.createCriteria();
        // where and username = ?
        criteria.andUsernameEqualTo("admin");
        // where and password = ?
        criteria.andPasswordEqualTo("admin");
        // 执行SQL
        List<TbUser> list = mapper.selectByExample(tbUserExample);
        for (TbUser tbUser : list) {
            System.out.println(tbUser);
        }
    }


    @Test
    public void mybatisGeneratorUpdate() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = factory.openSession();

        // 基于SqlSession获取指定Mapper的代理对象
        TbUserMapper mapper = sqlSession.getMapper(TbUserMapper.class);
        TbUser user = new TbUser();
        user.setId(3);
        user.setEmail("1231231231312@qq.com");
        mapper.updateByPrimaryKeySelective(user);

        sqlSession.commit();
    }
}
